#!/bin/bash
function sys_optimize() {
go_sysOpt=
echo -e "${blue}当前正在执行的操作为:${nocolor} ${green}优化系统${nocolor}"
echo -ne "
此选项将会执行以下操作:
--------------------
${green}1.${nocolor}更改系统内核
${green}2.${nocolor}修改limits资源限制
${green}3.${nocolor}禁用SELinux
${green}4.${nocolor}修改hosts文件
${green}5.${nocolor}更换YUM源为阿里
${green}6.${nocolor}设置时区为中国上海
${green}7.${nocolor}同步系统时间
${green}8.${nocolor}安装基础软件
-------------------
"
while [[ ! $go_sysOpt == [YyNn] ]]
do
    echo -ne "继续[Yy,Enter],退出[Nn]: "
    read  go_sysOpt
    go_sysOpt=${go_sysOpt:-Y}
    echo  ""
done

if [[ $go_sysOpt != [Yy] ]];then
   return 1
fi

echo ""
echo -e "${cyan}开始执行系统优化${nocolor}"
echo ""
sleep 0.15

####修改内核
echo -e "${Info} ${purple}修改内核参数${nocolor}"
all_para="net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_timestamps = 1
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.somaxconn = 1024
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 196608 262144 393216
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog = 2500
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_synack_retries = 3
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 1024 65535
net.nf_conntrack_max = 1048576
net.ipv4.tcp_fin_timeout = 40
fs.file-max = 655350
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 50000000
fs.inotify.max_queued_events = 327679
vm.min_free_kbytes = 65536
vm.swappiness = 0"
#删除之前存在的配置
all_para_keys=${all_para// /}
for key in $all_para_keys
do
  para_key=$(echo $key|awk -F"=" '{print $1}')
  sed -i "/$para_key/d" /etc/sysctl.conf
done

#添加新的配置

cat >> /etc/sysctl.conf <<EOF
$all_para
EOF
modprobe ip_conntrack
echo -e "$Info ${purple}使用sysctl -p使内核修改生效${nocolor}" 
sysctl -p >/dev/null

####修改limits####
echo -e "$Info ${purple}系统limits限制修改${nocolor}"
sed -i '/^* *[a-z]* *nofile *[0-9]*$/d' /etc/security/limits.conf
sed -i  '$i *                soft    nofile          65535'  /etc/security/limits.conf
sed -i  '$i *                hard    nofile          65535'  /etc/security/limits.conf
for limits_file in `ls /etc/security/limits.d/[0-9]*-nproc.conf`
do
    sed -i '/^* *[a-z]* *nproc *.*/d' $limits_file
    sed -i '/^root *[a-z]* *nproc *.*/d' $limits_file
    sed -i '$ a root        soft    nproc     unlimited' $limits_file
    sed -i '$ a root        hard    nproc     unlimited' $limits_file
    sed -i '$ a *          soft    nproc     137216' $limits_file
    sed -i '$ a *          hard    nproc     137216' $limits_file
done
    
####禁用selinux####
echo -e "$Info ${purple}禁用SELinux${nocolor}"
sed -i 's/^SELINUX=[a-zA-Z]*/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/^SELINUX=[a-zA-Z]*/SELINUX=disabled/' /etc/selinux/config

####修改hosts文件####
echo -e "$Info ${purple}修改hosts文件${nocolor}"
sed -i '/^127.0.0.1/d' /etc/hosts
sed -i "/^$ipaddr *`hostname`$/d" /etc/hosts
sed -i "1i $ipaddr   `hostname`" /etc/hosts

###更换yum源为阿里云####
echo -e "$Info ${purple}修改YUM源为阿里云${nocolor}"
if [ "$die_network" == "false" ];then
    if [ -f /etc/yum.repos.d/CentOS-Base.repo ];then
        /bin/mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    fi
    if [ -f /etc/yum.repos.d/epel.repo ];then
        /bin/mv -f /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
    fi
    if [ -f /etc/yum.repos.d/epel-testing.repo ];then
        /bin/mv -f /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
    fi
    
    if [ "$system_ver" == "6" ];then
        curl -so /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
        curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
    elif [ "$system_ver" == "7" ];then
        curl -so /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
        curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    elif [ "$system_ver" == "8" ];then
        curl -so /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
        yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
        sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
        sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
    fi
fi
    
####修改时区####
echo -e "$Info ${purple}修改时区为中国上海${nocolor}"
date -R |grep "+0800" >/dev/null 2>&1
if [ "$?" != "0" ];then
   rm -rf /etc/localtime
   ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
fi

echo -e "$Info ${purple}安装基础软件${nocolor}"
yum -y install openssh-clients wget ntpdate unzip zip bzip2 net-tools gcc gcc-c++ glibc qrencode 2>&1
echo -e "$Info ${purple}同步NTP时间${nocolor}"
ntpdate time.pool.aliyun.com
hwclock --systohc

echo ""
echo -ne "${cyan}系统优化运行完毕，按任意键返回!${nocolor}"
read -s -n1 goback
echo -e ""
}